package com.stars.controller.job;
import com.stars.common.enums.JobOperateEnum;
import com.stars.entity.job.MyScheduleJobEntity;
import com.stars.services.job.MyScheduleJobAccessServices;
import com.stars.services.job.MyScheduleJobConfigServices;
import com.stars.entity.dto.job.MyScheduleJobDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.stars.common.annotation.Log;
import com.stars.common.base.BaseMsg;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.controller.BaseController;
import com.stars.common.enums.OperationType;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import com.stars.common.valid.AddView;
import com.stars.common.valid.UpdateView;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.annotation.Validated;

/**
 * @Title: 任务调度 mybatis dao
 * @author dj
 * @date 2019-10-12 13:54:31
 * @version V1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/myScheduleJobCtl")
@Api(tags ="任务调度")
public class MyScheduleJobCtl extends BaseController{
   @Autowired
   private MyScheduleJobAccessServices  myScheduleJobAccessServicesImpl;
   @Autowired
   private MyScheduleJobConfigServices  myScheduleJobConfigServicesImpl;

   /**
   *  分页查询
   * @param pageNum 起始页
   * @param pageSize 大小
   * @return 分页数据
   * @throws Exception
   */
   @GetMapping("/pageList")
   @ApiOperation(value = "任务调度"+BaseMsg.MODULAR_PAGE,notes ="任务调度"+ BaseMsg.MODULAR_PAGE)
   @Log(detail = "任务调度"+ BaseMsg.MODULAR_PAGE,operationType = OperationType.SELECT)
   public PageInfo<MyScheduleJobDTO> pageMyScheduleJobList(Integer pageNum, Integer pageSize, MyScheduleJobDTO dto,HttpServletRequest request) throws Exception{
        return myScheduleJobAccessServicesImpl.pageList(pageNum,pageSize,dto);

   }

    /**
    * 保存
    * @param entityName
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/add")
    @ApiOperation(value = "任务调度"+ BaseMsg.MODULAR_ADD,notes = "任务调度"+BaseMsg.MODULAR_ADD)
    @Log(detail = "任务调度"+BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    public ResultMsg addMyScheduleJob(@Validated({AddView.class}) MyScheduleJobEntity entityName,HttpServletRequest request) throws Exception{
        setAddBaseApp(entityName,request);
         myScheduleJobConfigServicesImpl.add(entityName);
         return ResultMsg.success();
    }
    /**
     * 保存
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
    @PostMapping(value = "/addJob")
    @ApiOperation(value = "添加任务调度"+ BaseMsg.MODULAR_ADD,notes = "任务调度"+BaseMsg.MODULAR_ADD)
    @Log(detail = "添加任务调度"+BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    public ResultMsg addJob(String ids) throws Exception{
       String [] idArray = ids.split(BaseConstant.SPOT);
        myScheduleJobConfigServicesImpl.addJob(idArray);
        return ResultMsg.success();
    }
    /**
    * 更新
    * @param updateEntityDto
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/update")
    @ApiOperation(value = "任务调度"+ BaseMsg.MODULAR_UPDATE,notes = "任务调度"+ BaseMsg.MODULAR_ADD)
    @Log(detail = "任务调度"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg updateMyScheduleJob(@Validated({UpdateView.class}) MyScheduleJobEntity updateEntityDto,HttpServletRequest request) throws Exception{
        MyScheduleJobEntity entity = myScheduleJobAccessServicesImpl.selectById(updateEntityDto.getId());
        ObjectCopyUtils.copyPropertiesIf(updateEntityDto,entity, BaseConstant.UPDATE);
        setUpdateBase(entity,request);
        return myScheduleJobConfigServicesImpl.updateSelectiveById(entity);
    }

    /**
    * 根据id查询
    * @param id
    * @return 查询对象
    * @throws Exception
    */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "任务调度"+ BaseMsg.MODULAR_FIND_ID,notes = "任务调度"+BaseMsg.MODULAR_FIND_ID)
    @Log(detail = "任务调度"+ BaseMsg.MODULAR_FIND_ID,operationType = OperationType.SELECT)
    public ResultMsg findMyScheduleJobById(String id) throws Exception{
        MyScheduleJobEntity entityName = myScheduleJobAccessServicesImpl.selectById(id);
         return ResultMsg.success(entityName);
    }

     /**
     * 删除
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/delById")
     @ApiOperation(value = "任务调度"+BaseMsg.MODULAR_DEL,notes ="任务调度"+BaseMsg.MODULAR_DEL)
     @Log(detail ="任务调度"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
     public ResultMsg delMyScheduleJobById(String ids) throws Exception{
         String [] strArray = ids.split(BaseConstant.SPOT);
         for (String id : strArray) {
             if(StringUtils.isNotBlank(id)){
                 MyScheduleJobEntity entityName = myScheduleJobAccessServicesImpl.selectById(id);
                 //如果任务状态不是删除,不能删除
                 if(!JobOperateEnum.DELETE.getValue().equals(entityName.getStatusJob())){
                     throw new RuntimeException("如果任务状态不是删除,不能删除");
                 }
             }
         }
         return myScheduleJobConfigServicesImpl.deleteById(ids);
     }

    @PostMapping("/start")
    @ApiOperation(value = "启动定时"+ BaseMsg.MODULAR_UPDATE,notes = "启动定时"+ BaseMsg.MODULAR_ADD)
    @Log(detail = "启动定时"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg start(String ids,HttpServletRequest request) throws Exception{
        MyScheduleJobEntity updateEntityDto = new MyScheduleJobEntity();
        setUpdateBase(updateEntityDto,request);
        for(String id:ids.split(BaseConstant.SPOT)){
            updateEntityDto.setId(id);
            myScheduleJobConfigServicesImpl.start(updateEntityDto);
        }
        return ResultMsg.success("启动定时任务成功");
    }

    @PostMapping("/pause")
    @ApiOperation(value = "暂停定时任务"+ BaseMsg.MODULAR_UPDATE,notes = "暂停定时任务"+ BaseMsg.MODULAR_ADD)
    @Log(detail = "暂停定时任务"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg pause(String ids,HttpServletRequest request) throws Exception{
        MyScheduleJobEntity updateEntityDto = new MyScheduleJobEntity();
        setUpdateBase(updateEntityDto,request);
        for(String id:ids.split(BaseConstant.SPOT)){
            updateEntityDto.setId(id);
            myScheduleJobConfigServicesImpl.pause(updateEntityDto);
        }
        return ResultMsg.success("暂停定时任务成功");
    }

    @PostMapping("/delJobById")
    @ApiOperation(value = "删除定时任务"+ BaseMsg.MODULAR_UPDATE,notes = "删除定时任务"+ BaseMsg.MODULAR_ADD)
    @Log(detail = "删除定时任务"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg delete(String ids,HttpServletRequest request) throws Exception{
        MyScheduleJobEntity updateEntityDto = new MyScheduleJobEntity();
        setUpdateBase(updateEntityDto,request);
        for(String id:ids.split(BaseConstant.SPOT)){
            updateEntityDto.setId(id);
            myScheduleJobConfigServicesImpl.deleteById(updateEntityDto);
        }
        return ResultMsg.success("删除定时任务成功");
    }

    @PostMapping("/startAllJob")
    @ApiOperation(value = "启动所有定时任务"+ BaseMsg.MODULAR_UPDATE,notes = "启动所有定时任务"+ BaseMsg.MODULAR_UPDATE)
    @Log(detail = "启动所有定时任务"+ BaseMsg.MODULAR_SEARCH,operationType = OperationType.SELECT)
    public ResultMsg startAllJob() throws Exception{
        myScheduleJobConfigServicesImpl.startAllJob();
        return ResultMsg.success("启动所有定时任务成功");
    }

    @PostMapping("/pauseAllJob")
    @ApiOperation(value = "暂停所有定时任务"+ BaseMsg.MODULAR_UPDATE,notes = "暂停所有定时任务"+ BaseMsg.MODULAR_UPDATE)
    @Log(detail = "暂停所有定时任务"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg pauseAllJob() throws Exception{
        myScheduleJobConfigServicesImpl.pauseAllJob();
        return ResultMsg.success("暂停所有定时任务成功");
    }

}